package com.usian.ai.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.ai.common.Result;
import com.usian.ai.common.vo.QuestionLibReqVo;
import com.usian.ai.common.vo.QuestionLibSaveReqVo;
import com.usian.ai.mapper.QuestionLibMapper;
import com.usian.ai.pojo.QuestionLib;
import com.usian.ai.service.QuestionLibService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 12 10 08 55
 **/

@Service
public class QuestionLibServiceImpl
extends ServiceImpl<QuestionLibMapper, QuestionLib>
implements QuestionLibService {


    @Override
    public Result listQuestionLibs(QuestionLibReqVo questionLibReqVo) {

        Page<QuestionLib> questionLibPage =
                new Page<>(questionLibReqVo.getPageNum(), questionLibReqVo.getPageSize());

        QueryWrapper<QuestionLib> questionLibQueryWrapper = new QueryWrapper<>();
        questionLibQueryWrapper.lambda().eq(questionLibReqVo.getInterviewerId()!=null,QuestionLib::getInterviewerId, questionLibReqVo.getInterviewerId());
        questionLibQueryWrapper.lambda().like(StrUtil.isNotBlank(questionLibReqVo.getQuestion()),QuestionLib::getQuestion, questionLibReqVo.getQuestion());

        questionLibQueryWrapper.lambda().orderByDesc(QuestionLib::getId);


        Page<QuestionLib> questionLibPage1 = this.baseMapper.selectPage(questionLibPage, questionLibQueryWrapper);

        return Result.ok(questionLibPage1);
    }

    @Override
    public Result addQuestionLib(QuestionLibSaveReqVo questionLibSaveReqVo) {
        QuestionLib questionLib = new QuestionLib(
                questionLibSaveReqVo.getQuestion(),
                questionLibSaveReqVo.getReferenceAnswer(),
                questionLibSaveReqVo.getAiSrc(),
                questionLibSaveReqVo.getInterviewerId()
        );

        questionLib.setIsOn(1);
        questionLib.setCreateTime(new Date());
        questionLib.setUpdatedTime(new Date());


        int insert = this.baseMapper.insert(questionLib);
        return Result.ok(insert);


    }
}
